﻿using System;
using System.Drawing;

namespace Spencen.Mobile.UI.Animations
{
    public class BrushAnimation : Animation<SolidBrush>
    {
        private SolidBrush _currentBrush;

        public BrushAnimation( object target, string propertyName, TimeSpan duration )
            : base( target, propertyName, duration )
        {
        }

        public override SolidBrush CurrentValue
        {
            get
            {
                if ( _currentBrush != null )
                    _currentBrush.Dispose();

                var originalColor = OriginalValue.Color;
                var finalColor = FinalValue.Color;
                var currentColor = Color.FromArgb(
                    Math.Min( 255, Math.Max( 0, originalColor.R + (int)( ( finalColor.R - originalColor.R ) * AdjustedPercentageComplete ) ) ),
                    Math.Min( 255, Math.Max( 0, originalColor.G + (int)( ( finalColor.G - originalColor.G ) * AdjustedPercentageComplete ) ) ),
                    Math.Min( 255, Math.Max( 0, originalColor.B + (int)( ( finalColor.B - originalColor.B ) * AdjustedPercentageComplete ) ) ) );
                _currentBrush = new SolidBrush( currentColor );

                return _currentBrush;
            }
        }
    }
}
